<template>
  <div class="content-wrapper">
    <div class="content-container">
      <el-form :inline="true" :model="listQuery">
        <el-form-item label="培训分类">
          <el-select
            v-model="listQuery.category"
            size="mini"
            clearable
            @change="changeCategory"
          >
            <el-option
              v-for="item in list.category"
              :key="item.key"
              :label="item.value"
              :value="item.key"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="listQuery.date"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            size="mini"
            :default-time="['00:00:00', '23:59:59']"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            class="filter-item"
            size="mini"
            round
            type="primary"
            icon="el-icon-search"
            @click="searchDate"
          >
            搜索
          </el-button>
        </el-form-item>
      </el-form>
      <el-form :inline="true" :model="listQuery">
        <!-- <el-form-item> -->
        <el-button
          v-for="(item, index) in optionsArr"
          :key="item.id"
          round
          :type="item.type"
          size="mini"
          @click="getData(item, index)"
        >
          {{ item.value }}
        </el-button>
      </el-form>
      <el-table
        v-loading="listLoading"
        :data="
          listData.filter(
            (data) =>
              !search || data.name.toLowerCase().includes(search.toLowerCase())
          )
        "
        row-key="id"
        align="left"
        highlight-current-row
        height="85vh "
        default-expand-all
      >
        <el-table-column prop="startTime" label="培训日期" width="100" sortable>
          <template slot-scope="{ row }">
            <span v-if="row.startTime">{{
              $moment(row.startTime).format('YYYY-MM-DD')
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="trainClassifyName"
          label="培训分类"
          width="100"
        />

        <el-table-column prop="label" label="培训名称" mini-width="250" />
        <el-table-column prop="trainerUserName" label="培训讲师" width="150" />
        <el-table-column prop="status" label="状态" width="150" />
        <el-table-column prop="modeName" label="培训方式" width="100" />

        <el-table-column prop="typeName" label="培训类型" width="100" />

        <el-table-column prop="trainFormName" label="培训形式" width="100" />
        <el-table-column prop="assessName" label="考核方式" width="100" />
        <el-table-column prop="trainSite" label="地点" min-width="100" />

        <el-table-column
          prop="createTime"
          label="创建时间"
          sortable
          width="180"
        >
          <template slot-scope="{ row }">
            <span v-if="row.createTime">{{
              $moment(row.createTime).format('YYYY-MM-DD HH:mm:ss')
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="150px"
          fixed="right"
        >
          <template slot="header" slot-scope="scope">
            <el-input
              v-model="search"
              size="mini"
              placeholder="输入培训名称搜索"
            />
          </template>
          <template slot-scope="{ row }">
            <el-dropdown
              v-if="row.trainerUserId"
              trigger="click"
              class="filter-item"
            >
              <el-button type="text" class="el-icon-more" />
              <el-dropdown-menu slot="dropdown" class="user-dropdown">
                <el-dropdown-item @click.native="selfStudy(row)"
                  >开始自学</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog title="选择自学文档" :visible.sync="dialogVisible" class="mini">
      <template>
        <el-table
          class="dialog-table"
          :show-header="false"
          :data="documentList"
          style="width: 100%"
          @cell-click="toDocument"
        >
          <el-table-column prop="documentName" />
        </el-table>
      </template>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <LookDocument :push-data="pushData" />
  </div>
</template>

<script>
import LookDocument from '@/views/train/train_list_participant/components/LookDocument/index.vue'
import { listParticipate } from '@/api/ts/record'
import { outColumnDrop } from '@/views/train/js/concernMeSortable.js'
import { concern_me_formatter } from '@/views/train/js/formatter.js'
import { processTableTree } from '@/views/train/js/tabel_tree.js'
import { getTimeShortcuts, getEndDay } from '@/utils/index.js'

export default {
  components: { LookDocument },
  data() {
    return {
      listLoading: false,
      list: [],
      col: [],
      dropCol: [],
      typeList: {
        0: '普通培训',
        1: '文档培训'
      },
      modeList: {
        0: '自学',
        1: '实操',
        2: '试题考核'
      },
      statusList: { 0: '时间未到', 1: '已完成', 2: '未完成' },
      documentList: [],
      pushData: {},
      type: '',
      urldata: '',
      dialogVisible: false,
      listQuery: {
        timeBegin: this.$moment(getEndDay(0)).format('YYYY-MM-DD HH:mm:ss')
        // category: 'year'
      },
      temp: '',
      listData: [],
      search: '',
      optionsArr: []
    }
  },
  mounted() {
    this.getParticipation()
    this.getData()
  },
  updated() {
    outColumnDrop(this.sortable)
  },
  methods: {
    async getParticipation() {
      await processTableTree().then((res) => {
        this.list = res
      })
      this.optionsArr = getTimeShortcuts()

      // let { data } = await getParticipation()
      // this.list = data.trainByClassifyResponse
      // this.list = processTableTree()
    },
    selfStudy(row) {
      this.dialogVisible = true
      this.documentList = row.trainDocumentRelResponses
      this.type = row.type
    },
    toDocument(item) {
      this.dialogVisible = false
      window.open(
        'onlyoffice/studyDocument.html?&token=' +
          this.$store.getters.token +
          '&documentId=' +
          item.documentId +
          '&mode=view' +
          '&type=' +
          this.type +
          '&urldata=' +
          process.env.VUE_APP_BASE_API
      )
    },
    viewAttachment(row) {
      console.log(row)
    },
    rowClick(row, column, event) {
      console.log(row, column, event, 'row, column, event')
      this.load(row)
    },
    getData(row, index) {
      if (row) {
        this.listQuery.timeBegin = row.timeBegin
        this.listQuery.timeEnd = row.timeEnd
        this.optionsArr.forEach((item, i) => {
          if (i === index) {
            item.type = 'primary'
          } else {
            item.type = ''
          }
        })
      }
      const data = this.listQuery
      delete data.date
      this.listQuery = data
      listParticipate(this.listQuery).then((res) => {
        const data = concern_me_formatter(res)
        this.listData = data
      })
    },
    changeCategory(row) {
      this.getData()
    },
    searchDate() {
      const data = this.listQuery
      this.optionsArr.forEach((item, i) => {
        item.type = ''
      })
      if (data.date) {
        data.timeBegin = this.$moment(this.listQuery.date[0]).format(
          'YYYY-MM-DD HH:mm:ss'
        )
        data.timeEnd = this.$moment(this.listQuery.date[1]).format(
          'YYYY-MM-DD HH:mm:ss'
        )
      }
      listParticipate(data).then((res) => {
        const data = concern_me_formatter(res)
        this.listData = data
      })
    }
  }
}
</script>
<style lang="scss" scope>
.dialog-table {
  height: unset;
}
</style>
